/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trumpet.connection;

import java.awt.Component;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.Player;
import trumpet.App;
import trumpet.core.User;
import trumpet.gui.Message;
import trumpet.video.Manager;

/**
 *
 * @author vsemaphore
 */
public class Connection extends Thread {
    
    protected Socket _socket;
    protected BufferedReader _input;
    protected PrintStream _output;
    protected Message _window;
    protected User _user;
    
    private boolean _waitForLine = true;
    
    public Connection(Socket socket, User user)
    {
        this._socket = socket;
        if(user == null) {
            User u = App.mainWindow.getBuddyList().getByIp(socket.getInetAddress().toString().substring(1));
            if ( u == null) {
                u = new User("Gosc", null, socket.getInetAddress().toString().substring(1));
            }
            this._user = u;
        } else {
            this._user = user;           
        }
        
        this._window = new Message(this._user);
        this._window.setVisible(true);
        this._window.setConnection(this);
    }
    
    public void handle()
    {
        try {
            _input = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
            _output = new PrintStream(_socket.getOutputStream());
            start();
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void endConversation()
    {
        _waitForLine = false;
        _window.setVisible(false);
        try {
            _socket.close();
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void sendMessage(String message)
    {
        _output.println(message);
    }
    
    public void sendVideoStream()
    {
        String command = "\\video " + Manager.getInstance().getServerPort();
        _output.println(command);
    }
    
    protected void handleLine(String line)
    {
        if (line.charAt(0) == '\\') {
            System.out.println("Command: " + line);
            
            if (line.equals("\\end")) {
                endConversation();
            } else if (line.matches("\\\\video\\s+(.+)")) {
                String addr = line.split("\\s")[1];
                final int id = Integer.valueOf(addr).intValue();
                System.out.println("Video command: " + addr);
                
                Player player = Manager.getInstance().createPlayer(id);
                Component c = player.getVisualComponent();
                _window.setVideoComponent(c);
                    
            } else {
                System.err.println("Undefinded command: " + line);
            }
                        
        } else {
            _window.sendMessage(line);
            System.out.println("Talk: " + line);
        }
    }
    
    @Override
    public void run()
    {
        while (_waitForLine) {
            try {
                handleLine(_input.readLine());
            } catch (IOException ex) {
                Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        System.out.println("End of talk.");
    }
    
}
